import json
from django.urls import reverse
from rest_framework.test import APITestCase


class TextApiTests(APITestCase):
  def test_anekdot_view(self):
    '''
    Ensure endpoint returns text
    '''
    response = self.client.get('/api/text/randomAnekdot/').json()

    self.assertEqual(type(response['result']), str)

  def test_anarchy_view(self):
    '''
    Ensure endpoint returns correctly converted text
    '''
    response = self.client.get(
      '/api/text/anarchy/',
      {
        'text': 'тест'
      }
    ).json()

    self.assertEqual(response['result'], 'Т Е С Т')

  def test_synonym_view(self):
    '''
    Ensure endpoint returns correct synonyms
    '''
    response = self.client.get(
      '/api/text/synonym/',
      {
        'word': 'питон'
      },
    ).json()

    self.assertEqual(response['result'], ['змея', 'удав'])

  def test_antonym_view(self):
    '''
    Ensure endpoint returns correct antonyms
    '''
    response = self.client.get(
      '/api/text/antonym/',
      {
        'word': 'белый'
      },
    ).json()

    self.assertEqual(response['result']['antonyms'], ['чёрный', 'грязный'])


  def test_holidays_view(self):
    '''
    Ensure endpoint returns holidays
    '''
    response = self.client.get(
      '/api/text/holidays/',
      {
        'day': 1,
        'month': 1,
      },
    ).json()

    self.assertEqual(
      response['result'][0],
      {
        'name': 'Новый год (New Year`s Day)',
        'day': 1,
        'month': 1
      }
    )

  def test_code_execution_view(self):
    '''
    Ensure endpoint executes Rust and returns correct dictionary
    '''
    response = self.client.get(
      '/api/text/executeCode/',
      {
        'language': 'rust',
        'code': 'fn main() { println!("test") }',
      },
    ).json()

    self.assertEqual(
      response['result']['stdout'],
      'test\n',
    )
    self.assertEqual(
      response['result']['stderr'],
      None,
    )
    self.assertNotEqual(
      response['result']['exec_time'],
      None,
    )
